package com.asktgapp.engineer.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.asktgapp.JGTool.Utils;
import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.model.AccountRecordVO;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.asktgapp.widget.RefreshRecyclerView;
import com.asktgapp.modulebase.common.adapter.BaseAdapter;
import com.asktgapp.modulebase.common.adapter.BaseViewHolder;

import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * time:2018/1/25
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:消费记录
 */

public class AccountRecordFragment extends BaseFragment {
//    @InjectView(R.id.recycle)
    RefreshRecyclerView mRefreshRecyclerView;
    private RecyclerView mRecyclerView;
    private BaseAdapter mBaseAdapter;
    private int mPage = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.freshrecyclerview, container, false);
        ButterKnife.inject(this, viewGroup);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().setBackNavigationIcon();
        mRefreshRecyclerView=rootView.findViewById(R.id.recycle);
        mRecyclerView = mRefreshRecyclerView.getRecyclerView();
        mRefreshRecyclerView.setLoadMoreAction(new RefreshRecyclerView.Action() {
            @Override
            public void onAction(int page) {
                mPage = page;
                getNetData(mPage);
            }
        });
        mRefreshRecyclerView.setRefreshAction(new RefreshRecyclerView.Action() {
            @Override
            public void onAction(int page) {
                mPage = page;
                getNetData(mPage);
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mBaseAdapter = new BaseAdapter(getActivity()) {
            @Override
            public BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
                BaseViewHolder holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_account_record, parent, false), new BaseViewHolder.Callbacks() {
                    @Override
                    public void bind(BaseViewHolder holder, Object data) {
                        if (data instanceof AccountRecordVO) {
                            AccountRecordVO bean = (AccountRecordVO) data;
                            TextView title = holder.getView(R.id.tv_state);
                            TextView content = holder.getView(R.id.tv_content);
                            if (bean.getMode() == 0) {
                                holder.setText(R.id.tv_with_draw_moenty, "+" + String.valueOf(bean.getMoney()));
                            } else {
                                holder.setText(R.id.tv_with_draw_moenty, "-" + String.valueOf(bean.getMoney()));
                            }
                            switch (bean.getType()) {
                                case 0:
                                    title.setText("即时咨询收入 ");
                                    content.setText(bean.getInfo());
                                    break;
                                case 1:
                                    title.setText("专业检车收入  ");
                                    content.setText(bean.getInfo());
                                    break;
                                case 2:
                                    title.setText("故障检测收入 ");
                                    content.setText(bean.getInfo());
                                    break;
                                case 3:
                                    title.setText("提现 ");
                                    if (bean.getTxType() == 0) {
                                        content.setText("支付宝  " + bean.getAccount().substring(0, 3) + "****" + bean.getAccount().substring(bean.getAccount().length() - 4));
                                    } else {
                                        content.setText("银行卡  " + bean.getBankName() + "  尾号" + bean.getAccount().substring(bean.getAccount().length() - 4));
                                    }
                                    if (bean.getAuthStatus() == 0) {
                                        title.setText("提现申请审核中");
                                    } else if (bean.getAuthStatus() == 1) {
                                        title.setText("提现申请处理中");
                                    } else if (bean.getAuthStatus() == 2) {
                                        title.setText("提现申请未通过");
                                    } else if (bean.getAuthStatus() == 3) {
                                        title.setText("提现申请处理成功");
                                    }

                                    break;
                                case 4:
                                    title.setText("提现手续费用 ");
                                    content.setText(bean.getInfo());
                                    break;
                                case 5://消费记录
                                    title.setText(bean.getInfo().substring(bean.getInfo().indexOf("-")+1));
                                    content.setText(bean.getInfo());
                                    break;

                                case 6://工程师端退款记录减钱

                                    String mContent = bean.getInfo().substring(bean.getInfo().indexOf("-") + 1);
                                    title.setText(mContent + "退款 ");
                                    content.setText(bean.getInfo());
                                    break;
                                case 7://打赏收入

                                    title.setText("打赏收入  ");
                                    content.setText(bean.getInfo());
                                    break;
                                case 8://上门服务收入
                                    title.setText("上门服务收入  ");
                                    content.setText(bean.getInfo());
                                    break;
                                case 9://预付款收入

                                    title.setText("预付款收入  ");
                                    content.setText(bean.getInfo());
                                    break;
                            }
                            holder.setText(R.id.tv_add_time, Utils.getDateAll(bean.getAddTime()));

                        }
                    }

                    @Override
                    public void onItemClick(View view, int position) {

                    }
                });
                return holder;
            }
        };
        mRecyclerView.setAdapter(mBaseAdapter);
    }

    @Override
    public void loadData() {
        visibleLoading();
        getNetData(mPage);
    }

    private void getNetData(final int mPage) {
        // 0:账户明细 1:提现记录
        Apiservice apiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
        params.put("type", 0);
        params.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
        params.put("page", mPage);
        Call<ApiResponseBody<List<AccountRecordVO>>> call = apiservice.tiXianList(params);
        call.enqueue(new Callback<ApiResponseBody<List<AccountRecordVO>>>() {
            @Override
            public void onResponse(Call<ApiResponseBody<List<AccountRecordVO>>> call, Response<ApiResponseBody<List<AccountRecordVO>>> response) {
                mRefreshRecyclerView.endPage();
                inVisibleLoading();
                if (response.isSuccessful()) {
                    List<AccountRecordVO> list = response.body().getResult();
//                    if (list.size() == 0) {
//                        visibleNoData();
//                  else {
//                            mBaseAdapter.setData(list);
//                        }  }
                    if (mPage == 1) {
                        if (list.size() == 0) {
                            visibleNoData();
                        } else {
                            mBaseAdapter.setData(list);
                            mRefreshRecyclerView.showNextMore(mPage);
                        }
                    } else {
                        if (list.size() == 0) {
                            mRefreshRecyclerView.showNoMore();
                        } else {
                            mBaseAdapter.addData(BaseAdapter.AddType.LASE, list);
                            mRefreshRecyclerView.showNextMore(mPage);
                        }
                    }
                } else {
                    showTost(response.raw().message(), 1);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody<List<AccountRecordVO>>> call, Throwable t) {
                mRefreshRecyclerView.endPage();
                inVisibleLoading();
                ApiException apiException = ExceptionHandle.handleException(t);
                if (apiException.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(apiException.getMessage(), 0);
                }
            }
        });
    }
}
